package com.woniuxy.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.store.infra.mapper.StoreInfoMapper;
import com.woniuxy.store.infra.model.StoreInfo;
import com.woniuxy.store.service.StoreInfoService;
import com.woniuxy.store.service.util.OSSUtil;
import com.woniuxy.store.service.util.QrCodeUtils;
import com.woniuxy.store.service.util.UploadFileUtil;
import exception.MallException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import result.CommonResultCodeEnum;

import java.io.IOException;
import java.time.LocalDate;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zz
 * @since 2023-07-20
 */
@Service
public class StoreInfoServiceImpl extends ServiceImpl<StoreInfoMapper, StoreInfo> implements StoreInfoService {


    @Autowired
    private OSSUtil ossUtil;

    @Autowired
    private UploadFileUtil uploadFileUtil;




    @Override
    public void uplicense(MultipartFile file, Integer storeId) throws IOException {
        String url = uploadFileUtil.uploadImg(file);
        StoreInfo storeInfo = getById(storeId);
        String userImg = storeInfo.getLicense();
        if (userImg!=null){
            int i = userImg.lastIndexOf("/");
            String name = userImg.substring(i);

            ossUtil.deleteFile("zjr-bucket",name.substring(1));
        }
        storeInfo.setLicense(url);
        updateById(storeInfo);
    }

    @Override
    public void uploadPermit(MultipartFile file, Integer storeId) throws IOException {
        String url = uploadFileUtil.uploadImg(file);
        StoreInfo storeInfo = getById(storeId);
        String userImg = storeInfo.getPermit();
        if (userImg!=null){
            String name = userImg.substring(userImg.lastIndexOf("/"));
            ossUtil.deleteFile("zjr-bucket",name.substring(1));
        }
        storeInfo.setPermit(url);
        updateById(storeInfo);
    }

    @Override
    public void modifyStoreInfo(StoreInfo storeInfo) {
        StoreInfo info = getById(storeInfo.getStoreId());
        if (info==null){
            throw new MallException("店铺不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        updateById(storeInfo);
    }

    @Override
    public void checkStoreExpire() {
        UpdateWrapper<StoreInfo> wrapper = new UpdateWrapper<>();
        LocalDate now = LocalDate.now();
        System.out.println(now);
        wrapper.gt("expiration_time",now);
        wrapper.set("store_state","ARREARS");
        wrapper.set("role_id",4);
        update(wrapper);
    }


}
